package cn.lhl01.sys.entity.vo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义的请求转化
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {

	private Map<String, String[]> params = new HashMap<String, String[]>();

	public ParameterRequestWrapper(HttpServletRequest request) throws IOException {
		super(request);
		this.params.putAll(request.getParameterMap());
	}

	/**
	 * 重载一个构造方法
	 *
	 * @param request
	 * @param extendParams
	 */
	public ParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> extendParams) throws IOException {
		this(request);
		addAllParameters(extendParams);
	}

	@Override
	public Enumeration<String> getParameterNames() {
		return Collections.enumeration(params.keySet());
	}

	@Override
	public String getParameter(String name) {
		String[] values = params.get(name);
		if (values == null || values.length == 0) {
			return null;
		}
		return values[0];
	}

	@Override
	public String[] getParameterValues(String name) {
		return params.get(name);
	}

	public void addAllParameters(Map<String, String[]> otherParams) {
		for (Map.Entry<String, String[]> entry : otherParams.entrySet()) {
			addParameter(entry.getKey(), entry.getValue());
		}
	}


	public void addParameter(String name, Object value) {
		if (value != null) {
			if (value instanceof String[]) {
				params.put(name, (String[]) value);
			} else if (value instanceof String) {
				params.put(name, new String[]{(String) value});
			} else {
				params.put(name, new String[]{String.valueOf(value)});
			}
		}
	}

	@Override
	public Map<String, String[]> getParameterMap() {
		return this.params;
	}

}
